package org.jeecg.modules.demo.cashier;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.demo.weixin.entity.SmtDev;
import org.jeecg.modules.demo.weixin.entity.SmtTrkList;
import org.jeecg.modules.demo.weixin.entity.SmtYuyueList;
import org.jeecg.modules.demo.weixin.entity.SmtYuyueUser;
import org.jeecg.modules.demo.weixin.smtdev.service.ISmtDevService;
import org.jeecg.modules.demo.weixin.smttrklist.service.ISmtTrkListService;
import org.jeecg.modules.demo.weixin.smtyuyuelist.service.ISmtYuyueListService;
import org.jeecg.modules.demo.weixin.smtyuyueuser.service.ISmtYuyueUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

/**
 * @Description: 收银台 预约排期表操作
 * @Author: jeecg-boot
 * @Date: 2023-10-23
 * @Version: V1.0
 */
@RestController
@RequestMapping("/weixin/cashieryuyuelist/cashierYuyueList")
@Slf4j
public class CashierYuyueListController extends JeecgController<SmtYuyueList, ISmtYuyueListService> {
    @Autowired
    private ISmtYuyueListService smtYuyueListService;
    @Autowired
    private ISmtYuyueUserService smtYuyueUserService;
    @Autowired
    private ISmtTrkListService smtTrkListService;
    @Autowired
    private ISmtDevService devService;

    /**
     * 查询所有排期接口
     *
     * @param smtYuyueList
     * @param current
     * @param pageSize
     * @param req
     * @return
     */
    @SneakyThrows
    @ApiOperation(value = "根据条件-分页列表查询", notes = "根据条件-分页列表查询")
    @GetMapping(value = "/queryPage")
    public Result<IPage<SmtYuyueList>> queryPage(SmtYuyueList smtYuyueList,
                                                 @RequestParam(name = "current", defaultValue = "1") Integer current,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                 HttpServletRequest req) {
        Page<SmtYuyueList> page = new Page<SmtYuyueList>(current, pageSize);
        IPage<SmtYuyueList> pageList = smtYuyueListService.queryPageList(page, smtYuyueList);
        return Result.OK(pageList);
    }

    /**
     * 根据id查询预约排期的详情
     *
     * @return
     */
    @SneakyThrows
    @GetMapping(value = "/queryYuYueById")
    public Result<?> queryYuYueById(String id) {
        SmtYuyueList smtYuyueList = smtYuyueListService.getById(id);
        QueryWrapper<SmtTrkList> trkListQueryWrapper = new QueryWrapper<>();
        trkListQueryWrapper.eq("trk_id", smtYuyueList.getTrkId());
        trkListQueryWrapper.eq("dev_code", smtYuyueList.getDevCode());
        SmtTrkList trkList = smtTrkListService.getOne(trkListQueryWrapper);
        if (smtYuyueList.getOrd().contains("X")){
            smtYuyueList.setTrkPrice(smtYuyueList.getCurrentPrice());
        }else {
            //根据排期id查询出当前排期玩法的价格
            if (trkList != null) {
                smtYuyueList.setTrkPrice(BigDecimal.valueOf(trkList.getTrkPrice()));
            }
        }
        //查询场次对应的人员
        QueryWrapper<SmtYuyueUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.in("status", 0,2);
        List<SmtYuyueUser> list = smtYuyueUserService.list(queryWrapper);
        smtYuyueList.setUserList(list);
        if (trkList != null) {
            smtYuyueList.setProductId(trkList.getProductId());
            smtYuyueList.setPackagePrice(trkList.getPackagePrice());
            LambdaQueryWrapper<SmtDev> devLambdaQueryWrapper = new LambdaQueryWrapper<>();
            devLambdaQueryWrapper.eq(SmtDev::getDevCode,smtYuyueList.getDevCode());
            SmtDev devServiceOne = devService.getOne(devLambdaQueryWrapper);
            if (devServiceOne!=null){
                smtYuyueList.setDevName(devServiceOne.getDevName());
            }
        }
        return Result.OK(smtYuyueList);
    }

    /**
     * 生成指令
     * @param id
     * @return
     */
    @SneakyThrows
    @GetMapping(value = "/saveyuyueLink")
    public Result<?> saveyuyueLink(@RequestParam String id) {
        SmtYuyueList smtYuyueList = smtYuyueListService.getById(id);
        if (smtYuyueList == null) {
            return Result.error("场次不存在");
        }
        long l = System.currentTimeMillis()/1000;
        // 将时间戳转为当前时间
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(l), ZoneOffset.ofHours(8));
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime tenMinutesLater = now.plusMinutes(10);
        //判断时间是否过期  过期重新生成
        if(smtYuyueList.getLinkTime()==null){
            smtYuyueList.setYuyueLink(RandomUtil.randomNumbers(6));
            smtYuyueList.setLinkTime(tenMinutesLater);
            smtYuyueListService.updateById(smtYuyueList);
        }
        if (localDateTime.isAfter(smtYuyueList.getLinkTime())){
            smtYuyueList.setYuyueLink(RandomUtil.randomNumbers(6));
            smtYuyueList.setLinkTime(tenMinutesLater);
            smtYuyueListService.updateById(smtYuyueList);
        }
        return Result.ok(smtYuyueList);
    }
}
